package com.pmsystem.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pmsystem.dao.CommunityMapper;
import com.pmsystem.pojo.Community;
import com.pmsystem.pojo.CommunityExample;
import com.pmsystem.pojo.CommunityShow;
import com.pmsystem.pojo.CommunityExample.Criteria;
import com.pmsystem.pojo.User;
import com.pmsystem.pojo.Village;

/**
 * 
* <p>Title: CommunityService</p>  
* <p>Description:处理小区设施的业务层 </p>  
* @author 树昌  
* @date 2019年1月22日
 */
@Service
public class CommunityService {

	@Autowired
	CommunityMapper communityMapper;
	@Autowired
	UserService  userService;
	@Autowired
	VillageService villageService;
	
	/**
	 * 
	 * <p>Title: addCommunity</p>  
	 * <p>Description:添加一条小区记录 </p>  
	 * @author 树昌
	 * @param community
	 */
	public boolean addCommunity(Community community) {
		int row = communityMapper.insertSelective(community);
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: showCommunity</p>  
	 * <p>Description:查询所有的小区设施的信息 </p>  
	 * @author 树昌
	 * @return
	 */
	public List<CommunityShow> showCommunity(String keyword, Integer startRow, Integer pageSize,Integer CID) {
		
		CommunityExample communityExample = new CommunityExample();
		Criteria criteria = communityExample.createCriteria();
		criteria.andEnableEqualTo((byte) 1);
		//条件查询
		if(CID!=null) {
			criteria.andCommunityIdEqualTo(CID);
		}
		// 模糊查询
		if (keyword != null) {
			keyword = "%" + keyword + "%";
			criteria.andCommunityNameLike(keyword);
			Criteria criteria2=communityExample.or();
			criteria2.andCommunityTypeLike(keyword);
		}
		// 分页查询
		if (startRow != null && pageSize != null) {
			if (((startRow-1)*pageSize) >= 0 && pageSize >= 1) {
				communityExample.setStartRow((startRow-1)*pageSize);
				communityExample.setPageSize(pageSize);
			} else {
				return null;
			}
		}
		// 没有条件则为全查询

		List<Community> listcommunity = communityMapper.selectByExample(communityExample);
		List<User> listuser = userService.showUser();
		List<Village> listvillage = villageService.showVillage(null, null, null);
		List<CommunityShow> listcommunityshow = new ArrayList<>();
		
		for (Community community : listcommunity) {
			
			CommunityShow communityShow=new CommunityShow();
			communityShow.setCommunityId(community.getCommunityId());
			communityShow.setCommunityName(community.getCommunityName());
			communityShow.setCommunityType(community.getCommunityType());
			
			for (Village village : listvillage) {
				
				if(community.getBlgVillageId()==village.getVillageId()) {
					communityShow.setVillageName(village.getVillageName());
				}
			}
			
			if (community.getBlgUserId() != null) {
				for (User user : listuser) {
					if (community.getBlgUserId() == user.getUserId()) {
						communityShow.setUserName(user.getPresonName());
					}
				}
			}
			listcommunityshow.add(communityShow);
		}
		
		return listcommunityshow;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteCommunity</p>  
	 * <p>Description:删除一条社区的记录 </p>  
	 * @author 树昌
	 * @param community
	 */
	public boolean deleteCommunity(Community community) {
		
		CommunityExample communityExample = new CommunityExample();
		Criteria criteria = communityExample.createCriteria();
		criteria.andCommunityIdEqualTo(community.getCommunityId());
		community.setEnable((byte) 0);
		
		int row = communityMapper.updateByExampleSelective(community, communityExample);
		return row==1?true:false;
	}
	
	
	
	/**
	 * 
	 * <p>Title: updateCommunity</p>  
	 * <p>Description:修改一条社区信息的记录 </p>  
	 * @author 树昌
	 * @param community
	 */
	public boolean updateCommunity(Community community) {
		
		CommunityExample communityExample = new CommunityExample();
		Criteria criteria = communityExample.createCriteria();
		criteria.andCommunityIdEqualTo(community.getCommunityId());
		
		int row = communityMapper.updateByExampleSelective(community, communityExample);
		return row==1?true:false;
	}
}
